package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {
    @Autowired
    SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    private HashMap list = new HashMap<>();

    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes = setmealDishes.stream().map((one) -> {
            one.setSetmealId(setmealDto.getId());
            return one;
        }).collect(Collectors.toList());

        /*for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDto.getId());
        }*/
        //保存套餐和菜品的关联信息，操作setmeal_dish,执行insert操作
        setmealDishService.saveBatch(setmealDishes);
    }

    @Override
    public R pageWithDish(Integer page, Integer pageSize, String name) {
        //查出来基本信息
        IPage<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wq = new LambdaQueryWrapper<>();
        //添加查询条件，根据name进行like模糊查询
        wq.like(name != null, Setmeal::getName, name);
        //添加排序条件，根据更新时间降序排列
        wq.orderByDesc(Setmeal::getUpdateTime);
        this.page(pageInfo, wq);

        //获得数据
        List<Setmeal> records = pageInfo.getRecords();
        records = records.stream().map((one) -> {
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(one, dto);
            LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
            qw.eq(SetmealDish::getSetmealId, one.getId());
            List<SetmealDish> list = setmealDishService.list(qw);
            dto.setSetmealDishes(list);

            Long categoryId = one.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                //分类名称
                String categoryName = category.getName();
                dto.setCategoryName(categoryName);
            }
            return dto;
        }).collect(Collectors.toList());


        list.clear();

        list.put("total", pageInfo.getTotal());
        list.put("records", records);

        return R.success(list);
    }

    @Override
    public R getBySId(Long id) {

        SetmealDto setmealDto = new SetmealDto();

        Setmeal setmeal = super.getById(id);

        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(qw);

        setmealDto.setSetmealDishes(list);
        BeanUtils.copyProperties(setmeal, setmealDto);
        return R.success(setmealDto);
    }

    @Override
    public R updateWithDish(SetmealDto setmealDto) {
        //原始更新
        this.updateById(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        LambdaQueryWrapper<SetmealDish> qw = new LambdaQueryWrapper<>();
        qw.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(qw);

        setmealDishes = setmealDishes.stream().map((one) -> {
            one.setSetmealId(setmealDto.getId());
            return one;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

        return R.success("修改成功!");
    }


}
